package net.zhengxinyun.performance.App.appCtrl;

import net.zhengxinyun.performance.App.dto.IndexDTOs.WorkIndexDTO;
import net.zhengxinyun.performance.Util.StringUtils;
import net.zhengxinyun.performance.core.Result;
import net.zhengxinyun.performance.core.ResultUtils;
import net.zhengxinyun.performance.entity.WorkScoreEntity;
import net.zhengxinyun.performance.entity.WorkSupervisionEntity;
import net.zhengxinyun.performance.filter.WorkScoreFilter;
import net.zhengxinyun.performance.filter.WorkSupervisionFilter;
import net.zhengxinyun.performance.service.workSupervision.WorkScoreService;
import net.zhengxinyun.performance.service.workSupervision.WorkSupervisionService;
import org.junit.After;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/app/workindex")
public class WorkIndexCtrl extends  BaseCtrl {


    @Autowired
    private WorkSupervisionService workSupervisionService;
    @Autowired
    private WorkScoreService workScoreService;

    @PostMapping("/getworkIndex")
    public Result<HashMap<String, Object>> getworkIndex(@RequestBody WorkIndexDTO param) {


        WorkSupervisionFilter filter = new WorkSupervisionFilter();
        List<WorkSupervisionFilter> list = workSupervisionService.query(filter);
        List<WorkIndexDTO> datalist = new ArrayList<>();

        for (WorkSupervisionFilter item : list) {
            WorkIndexDTO dto = new WorkIndexDTO();
            dto.setCooperateUnit(item.getCooperateUnit());
            dto.setDateTime(item.getDateTime());
            dto.setCityResponUnit(item.getCityResponUnit());
            dto.setIndexName(item.getIndexName());
            dto.setLeadUnit(item.getLeadUnit());
            dto.setMaxPoint(item.getMaxPoint());
            dto.setMainUnit(item.getMainUnit());
            dto.setLeaderName(item.getLeaderName());
            dto.setId(item.getId());
            datalist.add(dto);
        }

        HashMap<String, Object> map = new HashMap<>();
        map.put("data", datalist);
        System.out.println("/getworkIndex");
        return ResultUtils.getSuccessResults(map);


    }

    /**
     * 指标增删改
     * @param param
     * @return
     */
    @PostMapping("/editWordIndex")
    public Result<String> editWorkIndex(@RequestBody WorkIndexDTO param)
    {

        String optionType = param.getOptionType();

        if(StringUtils.isBlank(optionType))
        {
            return  ResultUtils.getResults(Result.Status.ERROR,"optionType参数不能为空");
        }

        WorkSupervisionEntity entity = new WorkSupervisionEntity();
        entity.setCooperateUnit(param.getCooperateUnit());
        entity.setCityResponUnit(param.getCityResponUnit());
        entity.setDateTime(param.getDateTime());
        entity.setIndexName(param.getIndexName());
        entity.setMaxPoint(param.getMaxPoint());
        entity.setDepartCode(StringUtils.isBlank(param.getDepartcode())?"1092":param.getDepartcode());
        entity.setLeaderName(param.getLeaderName());


        switch (optionType){

            case "add":

                entity.setId(null);
                String str = workSupervisionService.insert(entity);

                break;

            case "update":
                entity.setId(param.getId());
                workSupervisionService.update(entity);
                break;
            case  "del":
                entity.setId(param.getId());
                workSupervisionService.delete(entity);
                break;

        }
        System.out.println("/editWordIndex");
        return  ResultUtils.getSuccessResults("操作成功");


    }

    //专项工作督查获取分数：
    @PostMapping("/workScore")
    public Result<HashMap<String, Object>> getIndexScore(@RequestBody WorkIndexDTO param)
    {
        String currentYear = getCurrentYear();
        WorkScoreFilter filter = new WorkScoreFilter();
        filter.setYear(currentYear);
        List<WorkScoreFilter> query = workScoreService.query(filter);

        HashMap<String,Object> datamap = new HashMap<>();
        datamap.put("data",query);
        System.out.println("/workScore");

        return ResultUtils.getSuccessResults(datamap);

    }

    //专项工作督查分数增删改查
    @PostMapping("/editWorkScore")
    public Result<String> editIndexScore(@RequestBody WorkIndexDTO param)
    {


        String optionType = param.getOptionType();

        WorkScoreEntity workScoreEntity = new WorkScoreEntity();
        workScoreEntity.setDepartCode(param.getDepartcode());
        workScoreEntity.setDateTime(param.getDateTime());
        // workScoreEntity.setScore(param.);
        workScoreEntity.setScore(param.getScore());
        workScoreEntity.setAudit(1+"");
        workScoreEntity.setDel(0);
        workScoreEntity.setFinalScore(param.getScore());
        workScoreEntity.setIndexId(param.getIndexId());
        switch (optionType)
        {
            case "add":
               workScoreEntity.setId(null);

                String insert = workScoreService.insert(workScoreEntity);
                if("添加成功".equals(insert))
                {
                    return ResultUtils.getResults(Result.Status.ERROR, "无数据更新");
                }

                break;
            case "update":
                workScoreEntity.setId(param.getId());
                String update = workScoreService.update(workScoreEntity);
                if("修改成功".equals(update))
                {
                    return ResultUtils.getResults(Result.Status.ERROR, "无数据更新");
                }
                break;
     /*       case "del":
                workScoreEntity.setId(param.getId());
                String delete = workScoreService.delete(workScoreEntity);
                if("删除成功".equals(delete))
                {
                    return ResultUtils.getResults(Result.Status.ERROR, "无数据更新");
                }
                break;*/

        }
        System.out.println("/editWorkScore");

        return  ResultUtils.getSuccessResults("操作成功");

    }



}
